using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
//Written By: Jason Deatherage August 2008 Phoenix, AZ Home of the blazing inferno.
namespace SlideShowGenerator
{
    public partial class XMLCreator : Form
    {
        public XMLCreator()
        {
            InitializeComponent();
        }

        private void btnCreateXML_Click(object sender, EventArgs e)
        {
            if (txtAlbumTitle.Text == "")
            {
                MessageBox.Show("Please enter album title.");
            }
            else if (txtAlbumDescription.Text == "")
            {
                MessageBox.Show("Please enter ablum description.");
            }
            else if (txtPictureSource.Text == "")
            {
                MessageBox.Show("Please select a path for pictures.");
            }
            else if (txtOnlineFilePath.Text == "")
            {
                MessageBox.Show("Please enter a online file path for pictures.");
            }
            else if (txtThumbnailSource.Text == "")
            {
                MessageBox.Show("Please select a path for thumbnails.");
            }
            else if (txtOnlineFilePathThumbnail.Text == "")
            {
                MessageBox.Show("Please enter a online file path for thumbnails.");
            }
            else
            {
                WriteXMLFile();
            }
        }

        private void WriteXMLFile()
        {
            string strFileExt;
            try
            {
                
                XmlTextWriter objXmlTextWriter = new XmlTextWriter(txtPictureSource.Text +  "\\" + txtAlbumTitle.Text + ".xml", null);
                objXmlTextWriter.Formatting = Formatting.Indented;
                objXmlTextWriter.WriteStartDocument();
                objXmlTextWriter.WriteStartElement("data");
                objXmlTextWriter.WriteAttributeString("transition", "CrossFadeTransition");
                objXmlTextWriter.WriteStartElement("album");
                objXmlTextWriter.WriteAttributeString("title", txtAlbumTitle.Text);
                objXmlTextWriter.WriteAttributeString("description", txtAlbumDescription.Text);
                objXmlTextWriter.WriteAttributeString("image", txtOnlineFilePathThumbnail.Text + cbAlbumPhoto.SelectedItem.ToString());

                foreach (string f in Directory.GetFiles(txtPictureSource.Text))
                {
                    strFileExt = GetFileExtension(f);
                    if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                    {
                        objXmlTextWriter.WriteStartElement("slide");
                        objXmlTextWriter.WriteAttributeString("title", f.Substring(txtPictureSource.Text.Length + 1, f.Length - txtPictureSource.Text.Length - 1 - strFileExt.Length));
                        objXmlTextWriter.WriteAttributeString("description", txtAlbumDescription.Text);
                        objXmlTextWriter.WriteAttributeString("image", txtOnlineFilePath.Text + f.Substring(txtPictureSource.Text.Length + 1, f.Length - txtPictureSource.Text.Length - 1));
                        objXmlTextWriter.WriteAttributeString("thumbnail", txtOnlineFilePathThumbnail.Text + f.Substring(txtPictureSource.Text.Length + 1, f.Length - txtPictureSource.Text.Length - 1 - strFileExt.Length) + "T" + strFileExt);
                        objXmlTextWriter.WriteEndElement();
                    }
                }
       
                objXmlTextWriter.WriteEndElement();
                objXmlTextWriter.WriteEndDocument();
                objXmlTextWriter.Flush();
                objXmlTextWriter.Close();
                MessageBox.Show("The following file has been successfully created\r\n"
                    + txtPictureSource.Text + "\\" + txtAlbumTitle.Text + ".xml", "XML", MessageBoxButtons.OK,
                    MessageBoxIcon.Information);
                Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void btnPictureSource_Click(object sender, EventArgs e)
        {
            DialogResult result = fbXMLCreator.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtPictureSource.Text = fbXMLCreator.SelectedPath;
            }

        }

        private void btnThumbnailSource_Click(object sender, EventArgs e)
        {
            DialogResult result = fbXMLCreator.ShowDialog();
            if (result == DialogResult.OK)
            {
                txtThumbnailSource.Text = fbXMLCreator.SelectedPath;

                foreach (string f in Directory.GetFiles(txtThumbnailSource.Text))
                {
                    string strFileExt = GetFileExtension(f);
                    if (strFileExt.ToUpper() == ".JPG" || strFileExt.ToUpper() == ".GIF" || strFileExt.ToUpper() == ".PNG" || strFileExt.ToUpper() == ".BMP" || strFileExt.ToUpper() == ".JPEG" || strFileExt.ToUpper() == ".TIFF" || strFileExt.ToUpper() == ".TIF")
                        cbAlbumPhoto.Items.Add(f.Substring(txtThumbnailSource.Text.Length + 1, f.Length - txtThumbnailSource.Text.Length - 1));
                }
                cbAlbumPhoto.SelectedItem = cbAlbumPhoto.Items[0];
            }
        }

        //private void btnAlbumPhoto_Click(object sender, EventArgs e)
        //{
        //    OpenFileDialog result = new OpenFileDialog();
        //    result.Title = "Select album highlight photo.";
        //    result.InitialDirectory = @"c:\";
        //    result.Filter = "JPG (*.jpg)|*.jpg|JPEG (*.jpeg)|*.jpeg";
        //    if (result.ShowDialog() == DialogResult.OK)
        //    {
        //        txtAlbumImagePath.Text = result.FileName;
        //    }
        //}

        private string GetFileExtension(string fileName)
        {
            if (fileName.Substring(fileName.Length - 4).ToUpper() == ".JPG")
            {
                return ".JPG";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".GIF")
            {
                return ".GIF";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".PNG")
            {
                return ".PNG";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".BMP")
            {
                return ".BMP";
            }
            else if (fileName.Substring(fileName.Length - 5).ToUpper() == ".JPEG")
            {
                return ".JPEG";
            }
            else if (fileName.Substring(fileName.Length - 5).ToUpper() == ".TIFF")
            {
                return ".TIFF";
            }
            else if (fileName.Substring(fileName.Length - 4).ToUpper() == ".TIF")
            {
                return ".TIF";
            }
            else
            {
                return "";
            }
        }// End of GetFileExtension

    }
}